<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=name-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="container">
      <div class="box1">
        <div
          class="item"
          draggable="true"
          ondragstart="dragStart(event,'西瓜')"
        >
          西瓜
        </div>
        <div
          class="item"
          draggable="true"
          ondragstart="dragStart(event,'芒果')"
        >
          芒果
        </div>
        <div
          class="item"
          draggable="true"
          ondragstart="dragStart(event,'荔枝')"
        >
          荔枝
        </div>
      </div>
      <div class="box2" ondrop="drop(event)" ondragover="dragOver(event)"></div>
    </div>

    <script>
      const box2 = document.querySelector('.box2')

      // 拖拽事件
      function dragStart(e, name) {
        e.dataTransfer.setData('name', name)
        console.log(e)
      }

      // 释放事件
      function drop(e) {
        e.preventDefault()

        const msg = e.dataTransfer.getData('name')
        const div = document.createElement('div')
        div.textContent = msg
        div.classList.add('item')
        box2.appendChild(div)
      }

      // 拖拽目标经过事件
      function dragOver(e) {
        // 阻止浏览器默认行为以便于放置
        e.preventDefault()
      }
    </script>

    <style>
      .container {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        width: 60%;
        height: 80vh;
        border: 1px solid #999;
        box-sizing: border-box;
        padding: 10px;
        margin: 0 auto;
      }

      .box1,
      .box2 {
        width: 250px;
        height: 100%;
        border: 1px solid #999;
      }

      .box2 {
        overflow: hidden;
      }

      .item {
        width: 100px;
        height: 45px;
        line-height: 45px;
        background-color: blanchedalmond;
        text-align: center;
        margin: 10px;
      }
    </style>
  </body>
</html>
